/**
	License:		Artistic license 2.0 (see license.txt, also available online at http://www.perlfoundation.org/artistic_license_2_0)
	Authors:		OrbitalLab (http://www.orbitallab.ru/moonglide/), 2007-2009

	File:			MoonUtil.d
	Description:	some useful stuff
	Date:			16.08.2008 by Digited
**/
module moonglide.util.MoonUtil;

import
	tango.text.convert.Layout,
	tango.io.Console,

	moonglide.io.LogSystem,
	moonglide.math.MoonMath;


public import
	tango.text.Util;

typedef bool delegate( char[] n ) randGenTest;

/**
 * if "subject" is true then program halt with error message "msg"
 *
 * @authors: Digited
 */
void moonAssert( bool subject, char[] msg, char[] file, long line )
{
	try
	{
		assert( subject, msg );
	}
	catch( Exception e )
	{
		errorLog( Format( "{} in {} ({})", msg, file, line ));

		debug
		{
			consolePause;
		}
	}
}

/**
 * Call attention to console
 *
 * @authors: Jurec
 * @date: December 28, 2008
 */
void consolePause()
{
	Cout( "\nPress enter to continue..." ).newline;
	Cin.get;
}


/**
 * Generated unique Name by using some "bool test(char[] str)" func.
 * if function "bool test(char[] str)" returns true, then str is unique
 *
 * @authors: Jurec
 * @returns: Unique generated Name.
 * @date: December 3, 2008
 */
char[] uniqName(char[] str, randGenTest test )
{
    if( test(str) )
        return str;

    uint pos = locatePrior(str,'_');

    foreach( char a; str[pos..$] )
        if( a!='_')
        if(! ((a>='0') && (a<='9')) )
        {   // finded char
            pos = str.length;
            break;
        }

    str = str[0..pos];
    char[] uniq = str;
    uint num = 1;
    for(;;)
    {
        uniq = str ~ "_" ~ intToStr(num);
        if( test(uniq) )
            return uniq;
        ++num;
    }
}
